from flask import Flask
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
from flask_session import Session

from models import db
from utils.common import set_after_request_handle_fuc, icon_is_none, get_p_count
from views import index_blu, passport_blu, user_blu

app = Flask(__name__)

# 加载配置
app.config.from_pyfile("config.ini")

app.register_blueprint(index_blu)
app.register_blueprint(passport_blu)
app.register_blueprint(user_blu)

# 添加过滤器
app.add_template_filter(icon_is_none)
app.add_template_filter(get_p_count)

# 初始化数据库
db.init_app(app)
Session(app)

# 设置钩子函数，可以在调用视图函数之前自动执行
set_after_request_handle_fuc(app)

# 添加数据库迁移等工具
manager = Manager(app)
# 生成migrate对象 用来数据库迁移
migrate = Migrate(app, db)
# 添加db命令
manager.add_command('db', MigrateCommand)

if __name__ == '__main__':
    # app.run(port=9341)
    manager.run()
